/**
 * 文件名称 : SingleTouchHandler.java
 * <p>
 * 作者信息 : maidou
 * <p>
 * 创建时间 : 2011-11-24, 下午11:11:55
 * <p>
 * 版权声明 : Copyright (c) 2009-2012 CIeNET Ltd. All rights reserved
 * <p>
 * 评审记录 :
 * <p>
 */

package com.ttomato.game.framework.impl;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;
import android.view.View;

import com.ttomato.game.framework.Input;
import com.ttomato.game.framework.Input.TouchEvent;
import com.ttomato.game.framework.Pool;
import com.ttomato.game.framework.Pool.PoolObjectFactory;

/**
 * single touch handler used for android 1.5 and 1.6
 * <p>
 */
public class SingleTouchHandler implements TouchHandler
{
    boolean isTouched;
    
    int mTouhcX;
    
    int mTouchY;
    
    Pool<TouchEvent> mTouchEventPool;
    
    List<TouchEvent> mTouchEvents = new ArrayList<TouchEvent>();
    
    List<TouchEvent> mTouchEventsBuffer = new ArrayList<TouchEvent>();
    
    float mScaleX;
    
    float mScaleY;
    
    public SingleTouchHandler(View view, float scaleX, float scaleY)
    {
        PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>()
        {
            
            @Override
            public TouchEvent createObject()
            {
                // TODO Auto-generated method stub
                return new TouchEvent();
            }
        };
        
        mTouchEventPool = new Pool<Input.TouchEvent>(factory, 100);
        view.setOnTouchListener(this);
        
        mScaleX = scaleX;
        mScaleY = scaleY;
    }
    
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO Auto-generated method stub
        synchronized (this)
        {
            TouchEvent touchEvent = mTouchEventPool.newObject();
            
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    touchEvent.type = TouchEvent.TOUCH_DOWN;
                    isTouched = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    touchEvent.type = TouchEvent.TOUCH_DRAGGED;
                    isTouched = true;
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    touchEvent.type = TouchEvent.TOUCH_UP;
                    isTouched = false;
                    break;
                
                default:
                    break;
            }
            
            touchEvent.x = mTouhcX = (int) (event.getX() * mScaleX);
            touchEvent.y = mTouchY = (int) (event.getY() * mScaleY);
            mTouchEventsBuffer.add(touchEvent);
            return true;
        }
    }
    
    @Override
    public boolean isTouchDown(int pointer)
    {
        // TODO Auto-generated method stub
        synchronized (this)
        {
            if (pointer == 0)
            {
                return isTouched;
            }
            else
            {
                return false;
            }
        }
    }
    
    @Override
    public int getTouchX(int pointer)
    {
        // TODO Auto-generated method stub
        synchronized (this)
        {
            return mTouhcX;
        }
    }
    
    @Override
    public int getTouchY(int pointer)
    {
        // TODO Auto-generated method stub
        synchronized (this)
        {
            return mTouchY;
        }
    }
    
    @Override
    public List<TouchEvent> getTouchEvents()
    {
        // TODO Auto-generated method stub
        synchronized (this)
        {
            int len = mTouchEvents.size();
            for (int i = 0; i < len; i++)
            {
                mTouchEventPool.free(mTouchEvents.get(i));
            }
            mTouchEvents.clear();
            mTouchEvents.addAll(mTouchEventsBuffer);
            mTouchEventsBuffer.clear();
            return mTouchEvents;
        }
    }
    
}
